package titt.service.invoke;

import java.lang.reflect.Method;

import titt.type.Type;
import titt.type.TypedObject;
import titt.util.ArrayUtil;

public class ReflectionInvoker implements MethodInvoker {

	private final Object o;
	
	public ReflectionInvoker(Object o) {
		this.o = o;
	}
	
	@Override
	public TypedObject call(String name, TypedObject... args) throws Exception {
		Method m = o.getClass().getMethod(name, ArrayUtil.convertTo(args, new Class<?>[args.length], TypedObject.toClass));
		Type ret = new Type(m.getReturnType());
		Object[] oargs = ArrayUtil.convertTo(args, new Object[args.length], TypedObject.toObject);
		Object retVal = m.invoke(o, oargs);
		return new TypedObject(ret, retVal);
	}

}
